<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class AddressController extends Controller
{
    //收货地址列表
    public function index()
    {
        $uid = session('user') -> id;
        $data = DB::table('receiver') -> where('uid', $uid) -> get();
        return view('/home/address/index') -> with(['data' => $data]);

    }
    //添加收货地址
    public function insert(Request $request)
    {
        $data = $request -> except('_token');

        $address = $data['p'].' '.$data['c'].' '.$data['a'].' '.$data['address'];
        $data['address'] = $address;
        unset($data['p']);
        unset($data['c']);
        unset($data['a']);
        $res = DB::table('receiver') -> insert($data);
        if($res)
        {
            return redirect('/home/address/index');
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }
    }
    //设置默认收货地址
    public function selected($id)
    {
        $oldid = DB::table('receiver') -> where('selected',1) -> first() -> id;
        $res = DB::table('receiver') -> where('id', $id) -> update(['selected' => 1]);
        if(!$res)
        {
            return back() -> with(['info' => '设置失败']);
        }
        $res = DB::table('receiver') -> where('id', $oldid) -> update(['selected' => 0]);
        return redirect('/home/address/index');
    }
    public function delete($id)
    {
        $res = DB::table('receiver') -> where('id', $id) -> delete($id);
        if(!$res)
        {
            return back() -> with(['info' => '删除失败']);
        }
        return redirect('/home/address/index');
    }
    public function edit($id)
    {
        $uid = session('user') -> id;
        $data = DB::table('receiver') -> where('uid', $uid) -> get();
        $res = DB::table('receiver') -> where('id', $id) -> first();
        $a = explode(" ",$res -> address);
        $count = count($a);
        if($count == 3)
        {
            $res -> p =$a['0'];
            $res -> c =$a['1'];
            $res -> address = $a['2'];
        }else if($count == 4)
        {
            $res -> p = $a['0'];
            $res -> c = $a['1'];
            $res -> a = $a['2'];
            $res -> address = $a['3'];
        }
        // dd($res);
        return view('/home/address/index') -> with(['data' => $data, 'res' => $res, 'edit' => 1]);
    }
    public function update(Request $request)
    {

        $data = $request -> except('_token');
        $id = $data['id'];
        $address = $data['p'].' '.$data['c'].' '.$data['a'].' '.$data['address'];
        $data['address'] = $address;
        unset($data['p']);
        unset($data['c']);
        unset($data['a']);
        $res = DB::table('receiver') -> where('id', $id) -> update($data);
        if($res)
        {
            $uid = session('user') -> id;
            $data = DB::table('receiver') -> where('uid', $uid) -> get();
            return redirect('/home/address/index') -> with(['data' => $data]);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }

}
